package cn.rongcloud.im.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.rongcloud.im.R;
import cn.rongcloud.im.ui.activity.MoudleWebActivity;
import cn.rongcloud.im.ui.activity.NeighborhoodActivity;
import cn.rongcloud.im.ui.bean.MoudleChildBean;
import cn.rongcloud.im.ui.bean.MoudleGroupBean;
import cn.rongcloud.im.ui.moudle.manager.activity.ManagerActivity;
import cn.rongcloud.im.utils.ScreenUtils;

/**
 * Created by Administrator on 2017/3/17.
 */

public class MoudleAdapter extends RecyclerView.Adapter<MoudleAdapter.MyViewHolder> implements View.OnClickListener{
    /**上下文*/
    private Context mContext;
    /**存放MoudleGroup数据的List*/
    private List<MoudleChildBean> mMoudlChildList;
    /**子的ImageView的宽度*/
    private int mMoudleChildImageViewWidth;
    /**子的ImageView的高度*/
    private int mMoudleChildImageViewHeight;

    private float mScale = (float) (3 / 2.0);

    public MoudleAdapter(Context context ,List<MoudleChildBean> list){
        mContext = context;
        mMoudlChildList =list;
        initParams();
    }

    /**
     * 初始化参数
     */
    private void initParams() {
        //这里减1是为了防止3个ImageView的宽度相加刚好超过部分机型的宽度分辨率
        mMoudleChildImageViewWidth = (ScreenUtils.getScreenWidth(mContext)/3)-1;
        mMoudleChildImageViewHeight = (int) (mMoudleChildImageViewWidth / mScale);
    }

    @Override
    public MoudleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_moudle_child , null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MoudleAdapter.MyViewHolder holder, int position) {
        int imagePath ;
        String moudleName;
        boolean isSpace;
        imagePath = mMoudlChildList.get(position).getMoudleChildImage();
        moudleName =mMoudlChildList.get(position).getMoudleChildName();
        isSpace = mMoudlChildList.get(position).isSpace();


        ViewGroup.LayoutParams params = new ViewGroup.
                LayoutParams(mMoudleChildImageViewWidth , mMoudleChildImageViewHeight);
        holder.mContentLayout.setLayoutParams(params);
        if(imagePath == 0){
            Glide.with(mContext).load(R.color.white)
                    .asBitmap().into(holder.mMoudleImageView);
            holder.mContentLayout.setEnabled(false);
        }else{
            Glide.with(mContext).load(imagePath)
                    .asBitmap().into(holder.mMoudleImageView);
            holder.mContentLayout.setEnabled(true);
        }
        holder.mMoudleTextView.setText(moudleName);

        setLayoutClickListener(holder.mContentLayout , moudleName );
    }

    /**
     * 设置子Layout的点击事件
     * @param layout    子的父布局
     * @param moudleName    子moudle的名字
     */
    private void setLayoutClickListener(RelativeLayout layout, String moudleName) {
        layout.setTag(moudleName);
        layout.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mMoudlChildList.size();
    }

    @Override
    public void onClick(View view) {
        if ("邻里圈".equals(view.getTag())){
            mContext.startActivity(new Intent(mContext, NeighborhoodActivity.class));
            return;
        }
       if(view.getTag().equals("永辉微店")){
//           mContext.startActivity(new Intent(mContext , ConvenientStoreActivity.class));
           startWebActivity("http://ldapp.vvtrip.com/life/shop/1" , "永辉微店");
       }else if(view.getTag().equals("周黑鸭")){
           startWebActivity("http://ldapp.vvtrip.com/life/shop/2" , "周黑鸭");
       }else if(view.getTag().equals("e鲜果")){
           startWebActivity("http://ldapp.vvtrip.com/life/shop/3" , "e鲜果");
       }else if(view.getTag().equals("快克便利")){
           startWebActivity("http://ldapp.vvtrip.com/life/shop/4" , "快克便利");
       }else if(view.getTag().equals("星巴克")){
           startWebActivity("http://ldapp.vvtrip.com/life/shop/5" , "星巴克");
       }else if(view.getTag().equals("更多")){
           startWebActivity("http://ldapp.vvtrip.com/life/focus" , "更多");
       }else if(view.getTag().equals("活动")){
           startWebActivity("http://ldapp.vvtrip.com/life/activity" , "活动");
       }else if(view.getTag().equals("发现")){
           startWebActivity("http://ldapp.vvtrip.com/life/find" , "发现");
       }else if(view.getTag().equals("云停车")){
           Toast.makeText(mContext,mContext.getResources().getString(R.string.function_not_open),Toast.LENGTH_SHORT).show();
       }else if(view.getTag().equals("云视频")){
           Toast.makeText(mContext,mContext.getResources().getString(R.string.function_not_open),Toast.LENGTH_SHORT).show();
       }else if(view.getTag().equals("云对讲")){
           startWebActivity("http://ldapp.vvtrip.com/property/intercom" , "云对讲");
       }else if(view.getTag().equals("扫一扫")){
           Toast.makeText(mContext,mContext.getResources().getString(R.string.function_not_open),Toast.LENGTH_SHORT).show();
       }else if(view.getTag().equals("邻里圈")){
           startWebActivity("http://ldapp.vvtrip.com/life/moments" , "邻里圈");
       } else if(view.getTag().equals("物业缴费")){
           startWebActivity("http://ldapp.vvtrip.com/property/pay" , "物业缴费");
       }else if(view.getTag().equals("报事报修")){
           startWebActivity("http://ldapp.vvtrip.com/property/repair" , "报事报修");
       }else if(view.getTag().equals("物业公告")){
           startWebActivity("http://ldapp.vvtrip.com/property/bulletin" , "物业公告");
       }else if(view.getTag().equals("投诉建议")){
           startWebActivity("http://ldapp.vvtrip.com/property/advise" , "投诉建议");
       }else if(view.getTag().equals("蓝牙摇一摇")){
//           startWebActivity("http://ldapp.vvtrip.com/property/yy" , "蓝牙摇一摇");
           startWebActivity("http://ldapp.vvtrip.com/property/app" , "应用管理");
       }else if(view.getTag().equals("应用管理")){
           mContext.startActivity(new Intent(mContext , ManagerActivity.class));
       }

    }

    /**
     * 开启WebView的H5的Activity
     * @param url    跳转的地址
     * @param titleName    标题名字
     */
    private void startWebActivity(String url, String titleName) {
        Intent intent = new Intent(mContext , MoudleWebActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        bundle.putString("titleName", titleName);
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        /**Moudle的最外部Layout*/
        RelativeLayout mContentLayout;
        /**Moudle的ImageView*/
        ImageView mMoudleImageView;
        /**Moudle的TextView*/
        TextView mMoudleTextView;
        public MyViewHolder(View itemView) {
            super(itemView);
            mContentLayout = (RelativeLayout) itemView.findViewById(R.id.layout_moudle_child);
            mMoudleImageView = (ImageView) itemView.findViewById(R.id.iv_moudle_child);
            mMoudleTextView = (TextView) itemView.findViewById(R.id.tv_moudle_child);
        }
    }
}
